package com.etc.dao.impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.etc.dao.OrderDetailDao;
import com.etc.entity.Games;
import com.etc.entity.OrderDetail;
import com.etc.util.DBUtil;

public class OrderDetailDaoImpl implements OrderDetailDao{
	/**
	 * 创建一条订单详情orderDetail，订单号为0表示存在购物车中，未进行提交订单以及支付等后续操作
	 */
	@Override
	public int insert(OrderDetail orderDetail) {
		return DBUtil.update("insert into order_detail(order_no,games_id,customer_id) values (?,?,?)", orderDetail.getOrderNo(), orderDetail.getGamesId(), orderDetail.getCustomerId());
	}
	/**
	 * 通过用户编号和未提交订单号为0，查询用户先前加入购物车但未提交订单的游戏的订单详情，包括游戏信息，用于显示购物车
	 */
	@Override
	public List<Games> findByCustomerId(Integer customerId, String orderNo, Integer gamesId) {
		List<Games> list =  new ArrayList<Games>();
		StringBuilder sql = new StringBuilder("select games.games_id,games.games_name,games.price,games.type_id,games.status,games.image,games.content,games.business_id,games.onsale_time from order_detail left join games on order_detail.games_id = games.games_id where 1=1 ");
		if(customerId!=null) {
			sql.append(" and order_detail.customer_id = ").append(customerId);
		}
		if(orderNo!=null) {
			sql.append(" and order_detail.order_no = '").append(orderNo).append("'");
		}
		if(gamesId!=null) {
			sql.append(" and order_detail.games_id = ").append(gamesId);
		}
		
		ResultSet rs = DBUtil.query(sql.toString());
		try {
			while (rs.next()) {  
			        
				gamesId = rs.getInt("games_id");  
				String gamesName = rs.getString("games_name"); 
				BigDecimal price = rs.getBigDecimal("price"); 
				Integer typeId = rs.getInt("type_id");   
				Integer status = rs.getInt("status");   
				String image = rs.getString("image");     
				String content = rs.getString("content");   
				Integer businessId = rs.getInt("business_id");
				String onsaleTime = rs.getString("onsale_time");
				
				// 将每一条记录封装成一个Java对象
				Games games = new Games(); 
				games.setGamesId(gamesId);
				games.setGamesName(gamesName);
				games.setPrice(price);
				games.setTypeId(typeId);
				games.setStatus(status);
				games.setImage(image);
				games.setContent(content);
				games.setBusinessId(businessId);
				games.setOnsaleTime(onsaleTime);
				
				// 存储到集合中
				list.add(games);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 根据支付状态，游戏编号和用户编号查询已支付的订单
	 */
	@Override
	public OrderDetail findByStatusAndGamesIdAndCustomerId(Integer status, Integer gamesId, Integer customerId) {
		StringBuilder sql = new StringBuilder("select d.detail_id, d.order_no, d.games_id, d.customer_id, i.total, i.status, i.create_time, i.pay_time from order_detail d left join order_info i on i.order_no = d.order_no where 1=1 ");
		if(status!=null) {
			sql.append(" and i.status = ").append(status);
		}
		if(gamesId!=null) {
			sql.append(" and d.games_id = ").append(gamesId);
		}
		if(customerId!=null) {
			sql.append(" and d.customer_id = ").append(customerId);
		}
		
		
		ResultSet rs = DBUtil.query(sql.toString());
		
		OrderDetail orderDetail = null;
		try {
			while (rs.next()) { 
				Integer detailId = rs.getInt("detail_id");
				String orderNo = rs.getString("order_no");
				gamesId = rs.getInt("games_id");
				customerId = rs.getInt("customer_id");
				
				orderDetail = new OrderDetail();
				orderDetail.setGamesId(gamesId);
				orderDetail.setCustomerId(customerId);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return orderDetail;
	}
	
	
	/**
	 * 根据订单号，游戏编号，用户编号删除对应的订单详情
	 */
	@Override
	public int deleteByOrderNoAndGamesIdAndCustomerId(String orderNo, Integer gamesId, Integer customerId) {
		StringBuilder sql = new StringBuilder("delete from order_detail where 1=1 ");		
		if(orderNo!=null&&!"".equals(orderNo)) {
			sql.append(" and order_no = '").append(orderNo).append("'");
		}
		if(gamesId!=null) {
			sql.append(" and games_id = ").append(gamesId);
		}
		if(customerId!=null) {
			sql.append(" and customer_id = ").append(customerId);
		}
		
		return DBUtil.update(sql.toString());
	}

}
